package w83b.w83bOperacionesPaso.w83bIncidencias;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONObject;

import w83b.w83bBeans.W83bEscrutinioEqualsBean;
import w83b.w83bBeans.W83bListaDiferenciasEqualsBean;
import es.ejie.frmk.presentacion.operacion.paso.Q70ActionPasoOP;
import es.ejie.frmk.presentacion.sesiondatos.Q70ContextoEJIE;

// TODO: Auto-generated Javadoc
/**
 * The Class W83bTablaAgrupacionesOP.java.
 *
 * @author Deusto Sistemas
 */
public class W83bRecuperarEscrutinioDiferenciasOutOPAction extends Q70ActionPasoOP{

	
	protected void processOP(Q70ContextoEJIE contexto, ActionErrors errores, ActionMapping mapping,
			ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//Guardamos la accion en el contexto
	}

	protected ActionForward processForward(Q70ContextoEJIE contexto, ActionErrors errors, ActionMapping mapping, 
			ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

		JSONObject raiz = new JSONObject();
		JSONArray listaDnis = new JSONArray();
		
		List listaSindicatos = (List)contexto.get("w83bListaDnis");
		
		List escrutinios = (List)((HashMap)contexto.get("w83bHashMapSindicatoEscrutinio")).get("escrutinio");
		List diferencias = (List)((HashMap)contexto.get("w83bHashMapSindicatoEscrutinio")).get("diferencias");
		
		for(int i=6,size=listaSindicatos.size();i<size;i++){
			String sindicatoColegio = (String)listaSindicatos.get(i);
			
			String sindicato = sindicatoColegio.split("_%_")[1];
			String colegio = sindicatoColegio.split("_%_")[2];
			W83bEscrutinioEqualsBean escrutinioEquals = new W83bEscrutinioEqualsBean();//NOPMD
			escrutinioEquals.setCodSindicato(sindicato);
			escrutinioEquals.setCodColegio(colegio);
			
			int existeEscrutinio = escrutinios.indexOf(escrutinioEquals);
			
			if(-1==existeEscrutinio){
				W83bListaDiferenciasEqualsBean diferenciasEquals = new W83bListaDiferenciasEqualsBean();//NOPMD
				diferenciasEquals.setSindicato(sindicato);
				diferenciasEquals.setCodColegio(colegio);
				
				existeEscrutinio = diferencias.indexOf(diferenciasEquals);
				if(-1==existeEscrutinio){
					JSONObject objeto = new JSONObject();//NOPMD
					objeto.put("dniSindicatoInvalido", sindicatoColegio.split("_%_")[0]);
					listaDnis.put(objeto);
				}				
			}
		}
		
		raiz.put("listaDnis",listaDnis);
		

		
		response.setContentType("text/javascript;charset=iso-8859-1");
		response.setHeader("Pragma", "cache");
		response.setHeader("Expires", "0");
		response.setHeader("Cache-Control", "private");
		response.setStatus(HttpServletResponse.SC_OK);
		PrintWriter out = response.getWriter();
		out.print(raiz.toString());
		out.flush();
		
		return null;
	}
	


}
